﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HikPlayer
{
    public partial class PlayMP4 : Form
    {
        //uesed port;can be from 0 to 15;
        int PORT = 0;
        //视频文件路径
        private string m_strPlayFileName;

        //是否暂停
        private bool isPause;
        //快慢放标志
        int m_nSpeed;
        //截图
        DisplayCBFun DisCB;
        //
        public PlayMP4()
        {
            InitializeComponent();
        }

        private void PlayMP4_Load(object sender, EventArgs e)
        {
            //设置音量
            HikPlayer.Hik_PlayM4_SetVolume(PORT, 0xFFFF);

            HikPlayer.Hik_PlayM4_SetFileEndMsg(PORT, panel1.Handle, HikPlayer.WM_FILE_END);

            HikPlayer.Hik_PlayM4_SetEncChangeMsg(PORT, panel1.Handle, HikPlayer.WM_ENC_CHANGE);

        }

        private void btnPlayFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                m_strPlayFileName = openFileDialog1.FileName;
                if (!string.IsNullOrEmpty(m_strPlayFileName))
                {
                    OpenFile();
                    //设置画面质量
                    HikPlayer.Hik_PlayM4_SetPicQuality(PORT, true);                    
                }
                else
                    MessageBox.Show("请选择文件！！");
            }

        }
        private void OpenFile()
        {
            //打开播放文件
            HikPlayer.Hik_PlayM4_OpenFile(PORT, m_strPlayFileName);

            Play();
        }
        public void SetOpenState()
        {
            //m_bOpen=TRUE;
            int m_nWidth, m_nHeight;
            //获取原始图像的大小，可根据这个自动设置播放器大小
            HikPlayer.Hik_PlayM4_GetPictureSize(PORT, out m_nWidth, out m_nHeight);
            HikPlayer.Hik_PlayM4_SetOverlayMode(PORT, true, Color.FromArgb(255, 0, 255).ToArgb());
        }

        private void Play()
        {
            //开始播放
            HikPlayer.Hik_PlayM4_Play(PORT, panel1.Handle);//this.Handle);  
            //播放声音
            HikPlayer.Hik_PlayM4_PlaySound(PORT);
        }

        /// <summary>
        /// 播放
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPlay_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(m_strPlayFileName))
            {
                //是否暂停->播放
                if (isPause)
                {
                    HikPlayer.Hik_PlayM4_Pause(PORT, false);
                    isPause = false;
                }
                else
                    OpenFile();
            }

        }

        /// <summary>
        /// 暂停
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPause_Click(object sender, EventArgs e)
        {
            HikPlayer.Hik_PlayM4_Pause(PORT, true);
            isPause = true;
        }

        /// <summary>
        /// 停止
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            HikPlayer.Hik_PlayM4_Stop(PORT);
            HikPlayer.Hik_PlayM4_CloseFile(PORT);
            HikPlayer.Hik_PlayM4_RealeseDDraw();
            panel1.Invalidate(true);
        }

        private void btnFastForward_Click(object sender, EventArgs e)
        {
            if (HikPlayer.Hik_PlayM4_Fast(PORT))
            {
                m_nSpeed++;
                if (m_nSpeed > 0)
                    OnThrow2();
            }
        }

        private void btnFastBackward_Click(object sender, EventArgs e)
        {
            //慢速播放
            if (HikPlayer.Hik_PlayM4_Slow(PORT))
            {
                //timer1.Interval
                m_nSpeed--;
                if (m_nSpeed <= 0)
                    OnThrow0();
            }
        }
        /// <summary>
        /// 慢放
        /// </summary>
        public void OnThrow0()
        {
            HikPlayer.Hik_PlayM4_ThrowBFrameNum(PORT, 0);
        }

        /// <summary>
        /// 快进
        /// </summary>
        public void OnThrow2()
        {
            HikPlayer.Hik_PlayM4_ThrowBFrameNum(PORT, 2);
        }

        /// <summary>
        /// 开始位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnGotoStart_Click(object sender, EventArgs e)
        {
            HikPlayer.Hik_PlayM4_SetPlayPos(PORT, 0);
        }

        /// <summary>
        /// 末尾位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btnGotoEnd_Click(object sender, EventArgs e)
        {
            HikPlayer.Hik_PlayM4_SetPlayPos(PORT, 1);        
        }

        /// <summary>
        /// 截图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCapImage_Click(object sender, EventArgs e)
        {
            /*  代码说明：
               1. 这里和源代码有点出入，他用的是g_bCapPic变量来控制是否捕获图片，我用的是设置委托实例和null来达到。API说明：设置抓图回调函数；注意要尽快返回，如果要停止回调，可以把回调函数指针DisplayCBFun设为NULL。
             */
            DisCB = new DisplayCBFun(DisplayCBFun);
            HikPlayer.Hik_PlayM4_SetDisplayCallBack(PORT, DisCB);
            /*可以借鉴做视频流截取
             * 呵呵，搞定了 
                [DllImport("PlayCtrl.dll")] 
                public static extern bool PlayM4_GetJPEG(int nPort, byte[] pJpeg, int nBufSize, out int pJpegSize); 
                调用： 

                int pBufferSize = 1200*1600*3/2; 
                byte[] pDIBImage = new byte[pBufferSize]; 
                int tpBufferSize = 0; 
                HikPlayer.PlayM4_GetJPEG(g_iPort, pDIBImage, pBufferSize, out tpBufferSize); 
                if (tpBufferSize > 0) 
                { 
                FileStream fs = new FileStream(@"C:/123456789.jpg", FileMode.Create); 
                //开始写入 
                fs.Write(pDIBImage, 0, tpBufferSize); 
                //清空缓冲区、关闭流 
                fs.Flush(); 
                fs.Close(); 
                } 
             * */
        }
        /// <summary>
        /// 截图回调函数
        /// </summary>
        /// <param name="nPort"></param>
        /// <param name="pBuf"></param>
        /// <param name="nSize"></param>
        /// <param name="nWidth"></param>
        /// <param name="nHeight"></param>
        /// <param name="nStamp"></param>
        /// <param name="nType"></param>
        /// <param name="nReceaved"></param>
        public void DisplayCBFun(int nPort, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReceaved)
        {
            if (HikPlayer.Hik_PLayM4_ConvertToBmpFile(pBuf, nSize, nWidth, nHeight, nType, string.Format("D:\\capture{0}.bmp", nPort)))
            {
                MessageBox.Show("转换bmp失败！");
            }
            //停止回调
            HikPlayer.Hik_PlayM4_SetDisplayCallBack(PORT, null);
        }
    }
}
/*  代码说明：

                    1.     注意关于这两个函数API的说明：

                         Hik_PlayM4_Fast：快速播放，每次调用将使当前播放速度加快一倍，最多调用4次；要恢复正常播放调用Hik_PlayM4_Play(),从当前位置开始正常播放。

                         Hik_PlayM4_Slow：慢速播放，每次调用将使当前播放速度慢一倍；最多调用4次；要恢复正常播放调用Hik_PlayM4_Play。

 */
